﻿using MobileDesigner.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WebMatrix.WebData;
using PagedList;

namespace MobileDesigner.Controllers
{
    public class ClientsController : Controller
    {
        private MobileDesignerContext db = new MobileDesignerContext();

        //
        //GET: /Clients/ListClients

        [Authorize(Roles = "Administrator, Partner's Manager, Partner, Manager, Regional Manager")]
        public ViewResult ListClients(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.EmailSortParm = String.IsNullOrEmpty(sortOrder) ? "Email desc" : "";
            ViewBag.NameSortParm = sortOrder == "Name" ? "Name desc" : "Name";

            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;

            var clients = from u in db.Users
                          where u.SuperiorID == WebSecurity.CurrentUserId
                          select new ClientListModel {
                              ID = u.ID,
                              Email = u.Email,
                              Phone = u.Phone,
                              Name = u.LastName + " " + u.Name,
                          };

            if (!String.IsNullOrEmpty(searchString))
            {
                clients = clients.Where(u => u.Email.ToUpper().Contains(searchString.ToUpper())
                                       || u.Name.ToUpper().Contains(searchString.ToUpper()));
            }
            switch (sortOrder)
            {
                case "Name desc":
                    clients = clients.OrderByDescending(u => u.Name);
                    break;
                case "Email desc":
                    clients = clients.OrderByDescending(u => u.Email);
                    break;
                case "Name":
                    clients = clients.OrderBy(u => u.Name);
                    break;
                default:
                    clients = clients.OrderBy(u => u.Email);
                    break;
            }

            int pageSize = 10;
            int pageNumber = (page ?? 1);

            List<ClientListModel> clientList = new List<ClientListModel>();
            foreach (var item in clients)
            {
                if (Roles.IsUserInRole(item.Email, "Client"))
                    clientList.Add(item);
            }

            return View(clientList.ToPagedList(pageNumber, pageSize));
        }

        //GET ://Clients/ListFreeClients

        [Authorize(Roles = "Administrator, Partner's Manager, Partner, Manager, Regional Manager")]
        public ViewResult ListFreeClients(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.EmailSortParm = String.IsNullOrEmpty(sortOrder) ? "Email desc" : "";
            ViewBag.NameSortParm = sortOrder == "Name" ? "Name desc" : "Name";

            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;

            var freeClients = from u in db.Users
                              where u.SuperiorID == 0
                              select new ClientListModel { ID = u.ID, Email = u.Email, Name = u.LastName + " " + u.Name, Phone = u.Phone };

            if (!String.IsNullOrEmpty(searchString))
            {
                freeClients = freeClients.Where(u => u.Email.ToUpper().Contains(searchString.ToUpper())
                                       || u.Name.ToUpper().Contains(searchString.ToUpper()));
            }
            switch (sortOrder)
            {
                case "Name desc":
                    freeClients = freeClients.OrderByDescending(u => u.Name);
                    break;
                case "Email desc":
                    freeClients = freeClients.OrderByDescending(u => u.Email);
                    break;
                case "Name":
                    freeClients = freeClients.OrderBy(u => u.Name);
                    break;
                default:
                    freeClients = freeClients.OrderBy(u => u.Email);
                    break;
            }

            int pageSize = 10;
            int pageNumber = (page ?? 1);

            List<ClientListModel> freeClientList = new List<ClientListModel>();
            foreach (var item in freeClients)
            {
                if (!Roles.IsUserInRole(item.Email, "Administrator") && !Roles.IsUserInRole(item.Email, "Regional Manager"))
                    freeClientList.Add(item);
            }

            return View(freeClientList.ToPagedList(pageNumber, pageSize));
        }

        [Authorize(Roles = "Administrator, Partner's Manager, Partner, Manager, Regional Manager")]
        //POST ://Clients/AddFreeClient
        public ActionResult AddFreeClient(int id)
        {
            User addedUser = db.Users.Where(u => u.ID == id).FirstOrDefault();
            addedUser.SuperiorID = WebSecurity.CurrentUserId;
            db.SaveChanges();

            return RedirectToAction("ListClients");
        }
    }
}
